package com.elock.service.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.elock.base.core.Filter;
import com.elock.base.core.service.impl.BaseServiceImpl;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.ExceptionUtil;
import com.elock.base.core.util.WebUtils;
import com.elock.entity.CompanyInfo;
import com.elock.entity.Member;
import com.elock.entity.PcRole;
import com.elock.entity.PcUserRole;
import com.elock.service.CompanyInfoService;
import com.elock.service.LoginOutService;
import com.elock.service.MemberService;
import com.elock.service.PcUserRoleBaseService;

/**
 * Service - 登录登出
 * @author 仁德
 */
@Service("loginOutServiceImpl")
public class LoginOutServiceImpl extends BaseServiceImpl<Member>
		implements LoginOutService {

	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;
	@Resource(name = "memberServiceImpl")
	private MemberService memberService;
	@Resource(name = "pcUserRoleBaseServiceImpl")
	private PcUserRoleBaseService pcUserRoleBaseService;

	public void submitLogin(String username, String password, String companyName,
			HttpServletRequest request, HttpServletResponse response) {

		if (StringUtils.isEmpty(username)) {
			//请输入用户名
			ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3001"));
		}
		if (StringUtils.isEmpty(password)) {
			//请输入密码
			ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3002"));
		}
		Member member = null;
		CompanyInfo companyInfo = null;
		List<Filter> filters = new ArrayList<Filter>();
		if (StringUtils.isEmpty(companyName)) {
			member = memberService.findByUsername(username, null);
			if (member == null) {
				filters.clear();
				filters.add(Filter.isNotNull("companyInfoId"));
				filters.add(Filter.eq("username", username));
				List<Member> members = memberService.findList(2, filters, null);
				if (members.size() == 1) {
					member = members.get(0);
					companyInfo = companyInfoService.find(member.getCompanyInfoId());
				}
				else if (members.size() == 2) {
					//请输入企业编号、简称或全称
					ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3010"));
				}
			}
		}
		else {
			filters.clear();
			filters.add(Filter.eq("simple_name", companyName));
			companyInfo = companyInfoService.find(filters);
			if (companyInfo == null) {
				filters.clear();
				filters.add(Filter.eq("company_name", companyName));
				companyInfo = companyInfoService.find(filters);
				if (companyInfo == null) {
					filters.clear();
					filters.add(Filter.eq("company_code", companyName));
					companyInfo = companyInfoService.find(filters);
				}
			}
			if (companyInfo == null) {
				//根据输入的企业编号、简称或全称找不到企业
				ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3005"));
			}
			if (companyInfo.getIsEnabled() == null || !companyInfo.getIsEnabled()) {
				//该企业已禁用
				ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3009"));
			}
			member = memberService.findByUsername(username, companyInfo.getId());
		}
		if (member != null) {
			if (!member.getIsEnabled()) {
				//该账号已被禁用
				ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3006"));
			}
			//获取storeMember里面的密码
			if (!DigestUtils.md5Hex(password).equals(member.getPassword())) {
				//密码错误
				ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3007"));
			}
			member.setLoginIp(request.getRemoteAddr());
			member.setLoginDate(new Date());
			memberService.update(member);
		}
		else {
			//该用户不存在
			ExceptionUtil.throwServiceException(ConvertUtil.convertI18nMsg("3003"));
		}

		//查询是否企业管理员
		int isadmin = 0;
		filters.clear();
		filters.add(Filter.eq("member", member));
		if (companyInfo == null) {
			filters.add(Filter.isNull("companyInfoId"));
		}
		else {
			filters.add(Filter.eq("companyInfoId", companyInfo.getId()));
		}
		List<PcUserRole> pcUserRoles = pcUserRoleBaseService.findList(null,
				filters,
				null);
		for (PcUserRole pcUserRole : pcUserRoles) {
			PcRole pcRole = pcUserRole.getPcRole();
			if (pcRole.getCompanyInfoId() == null) {
				isadmin = 1;
				break;
			}
		}

		WebUtils.setPrincipal(member.getId(),
				member.getUsername(),
				companyInfo == null ? null : companyInfo.getId(),
				companyInfo == null ? null : companyInfo.getCompany_code(),
				"ChinaCN",
				isadmin);
	}
}
